package com.crm.service.statisticAnalysis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.model.common.dtos.PageResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.statisticAnalysis.dtos.ClientPageDTO;
import com.crm.model.statisticAnalysis.dtos.DoughnutChartDTO;
import com.crm.model.statisticAnalysis.pojos.ClientStatistics;
import com.crm.model.statisticAnalysis.vos.ClientDoughnutChartVO;
import com.crm.model.statisticAnalysis.vos.ClientLineChartVO;
import com.crm.model.statisticAnalysis.vos.DoughnutChartVO;
import com.crm.service.statisticAnalysis.mapper.ClientMapper;
import com.crm.service.statisticAnalysis.service.ClientService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientMapper clientMapper;

    /**
     * 客户分页查询
     *
     * @param dto
     */
    @Override
    public ResponseResult clientPage(ClientPageDTO dto) {
        //分页
        IPage<ClientStatistics> page = new Page(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<ClientStatistics> lqw = new LambdaQueryWrapper<>();
        //是否有归属人
        if (dto.getName() != null && !dto.getName().equals("")) {
            lqw.like(ClientStatistics::getBelonger, dto.getName());
        }
        //判断是否有渠道
        if (dto.getChannel() != null) {
            lqw.eq(ClientStatistics::getDepartment, dto.getDepartment());
        }
        //判断是否有归属部门
        if (!StringUtils.isBlank(dto.getDepartment())) {
            lqw.eq(ClientStatistics::getDepartment, dto.getDepartment());
        }
        page = clientMapper.selectPage(page, lqw);
        //封装返回
        PageResult pageResult = new PageResult();
        pageResult.setRecords(page.getRecords());
        pageResult.setTotal(page.getTotal());
        //返回数据
        return ResponseResult.okResult(pageResult);
    }

    /**
     * 客户统计折线图
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public ResponseResult clientLineChart(LocalDate begin, LocalDate end) {
        //时间集合
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //返回数据集合
        List<ClientLineChartVO> clientLineChartVOS = new ArrayList<>();
        //遍历每一天
        for (LocalDate date : dateList) {
            //一天最小时间 和 最大时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //新增用户数量
            Integer newClientList = clientMapper.newClientCount(beginTime, endTime);
            //总用户数量
            Integer totalClientList = clientMapper.totalClientCount(null, endTime);

            ClientLineChartVO lineChartVO = ClientLineChartVO.builder()
                    .newClientList(newClientList)
                    .totalClientList(totalClientList)
                    .date(date).build();

            clientLineChartVOS.add(lineChartVO);
        }


        return ResponseResult.okResult(clientLineChartVOS);
    }

    /**
     * 环形图
     *
     * @param queryTime
     * @return
     */
    @Override
    public ResponseResult doughnutChart(LocalDate queryTime) {
        //一天最小时间 和 最大时间
        LocalDateTime endTime = LocalDateTime.of(queryTime, LocalTime.MAX);
        //对应学科和每天新增
        List<DoughnutChartDTO> dtos = clientMapper.getSubjectNews(null, endTime);

        //返回集合VO
        DoughnutChartVO chartVO = new DoughnutChartVO();

        //封装数据对象集合
        List<ClientDoughnutChartVO> chartVOS = new ArrayList<>();

        //成交总用户
        Integer bargainCount = clientMapper.selectCount(null);
        //学科名称
        String SubjectList = StringUtils.join(dtos.stream().map(DoughnutChartDTO::getSubject).collect(Collectors.toList()), ",");
        int subjectSize = dtos.stream().map(DoughnutChartDTO::getSubject).collect(Collectors.toList()).size();
        //对应学科的数量
        String numberList = StringUtils.join(dtos.stream().map(DoughnutChartDTO::getNewClientNumber).collect(Collectors.toList()), ",");


        for (int i = 0; i < subjectSize; i++) {

            String[] split = SubjectList.split(",");
            String subject = split[i];

            String[] number = numberList.split(",");
            Integer totalClientNum = Integer.valueOf(number[i]);

            //成交占比 当天科目下成交人数 / 总的成交人数
            double proportion = 0.0;
            if (bargainCount != 0) {
                proportion = totalClientNum / bargainCount.doubleValue();
            }

            //封装数据
            ClientDoughnutChartVO doughnutChartVO = ClientDoughnutChartVO.builder()
                    .subject(subject)
                    .proportion(proportion)
                    .build();

            chartVOS.add(doughnutChartVO);

        }

        chartVO.setDoughnutChartList(chartVOS);


        chartVO.setDate(queryTime);
        //加入总成交客户
        chartVO.setBargainCount(bargainCount);
        //返回
        return ResponseResult.okResult(chartVO);
    }

    /**
     * 客户统计报表导出
     * @param response
     * @return
     */
    @Override
    public ResponseResult export(HttpServletResponse response) {
        


        return null;
    }
}
